<script setup lang="ts">
import { onMounted, ref } from "vue";
import { Api } from "../../assets/api/api";
import { useRouter, useRoute } from "vue-router";
import { showToast } from "vant";

//首次密码确认
const password = ref("");
//第二次密码确认
const cPassword = ref("");
//密码显示隐藏控制
const changePassword1 = ref("password");
const changePassword2 = ref("password");
const flag1 = ref(false);
const flag2 = ref(false);

//vue3自带方法
const router = useRouter();
const route = useRoute();

//控制密码显示隐藏
const passwordOneChange = () => {
  if (flag1.value) {
    changePassword1.value = "password";
    flag1.value = false;
  } else {
    changePassword1.value = "text";
    flag1.value = true;
  }
};
const passwordTwoChange = () => {
  if (flag2.value) {
    changePassword2.value = "password";
    flag2.value = false;
  } else {
    changePassword2.value = "text";
    flag2.value = true;
  }
};
//设置密码
const setPassword = () => {
  if (
    password.value.trim() != "" &&
    password.value.trim().length > 10 &&
    cPassword.value.trim() != "" &&
    password.value == cPassword.value
  ) {
    if (localStorage.getItem("user_information")) {
      let arr: Array<Object> = JSON.parse(
        window.localStorage.getItem("user_information") as any
      );
      localStorage.setItem(
        "user_information",
        JSON.stringify([
          {
            account: route.query.captcha,
            password: cPassword.value,
          },
          ...arr,
        ])
      );
      localStorage.setItem("price", "0"), router.push("/login");
    } else {
      localStorage.setItem(
        "user_information",
        JSON.stringify([
          {
            account: route.query.captcha,
            password: cPassword.value,
          },
        ])
      );
      localStorage.setItem("price", "0"), router.push("/login");
    }
  } else if (password.value.trim().length <= 10) {
    showToast("密码太短,最少为11个数字/符号/字母");
  } else {
    showToast("密码不相同");
  }
};
</script>

<template>
  <div class="setPassWord_view">
    <nav>
      <div class="app" @click="router.go(-1)"></div>
      <h1>设置密码</h1>
      <div class="sourch"></div>
    </nav>
    <div class="input_box">
      <div class="look_input">
        <input
          :type="changePassword1"
          placeholder="输入密码"
          v-model="password"
        />
        <div class="img" :class="{ con: flag1 }" @click="passwordOneChange">
          <van-image width="25" height="15" src="/img/home/look_password.png" />
        </div>
      </div>
      <div class="look_input">
        <input
          :type="changePassword2"
          placeholder="确认密码"
          v-model="cPassword"
        />
        <div class="img" :class="{ con: flag2 }" @click="passwordTwoChange">
          <van-image width="25" height="15" src="/img/home/look_password.png" />
        </div>
      </div>
    </div>
    <div class="btn">
      <button @click="setPassword">设置密码</button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.setPassWord_view {
  min-height: 100vmax;
  background-color: #fff;
}
nav {
  display: flex;
  justify-content: space-between;
  background-color: #f7f7f7;
  border-bottom: 1px solid #cbcbcb;
  .app {
    width: 17px;
    height: 17px;
    background: url(https://img.xmkanshu.com/operateimg/img/img/18721/18721.png)
      no-repeat 12px 0px;
    background-size: 8px 13px;
    margin-top: 13px;
    padding-left: 11px;
    padding-right: 44px;
  }
  h1 {
    font-size: 17px;
    color: #333333;
    padding-top: 12px;
    padding-bottom: 10px;
    font-weight: normal;
  }
  .sourch {
    display: flex;
    width: 60px;
    padding-right: 12px;
    justify-content: flex-end;
    align-items: center;
    p {
      font-size: 12px;
      color: #999999;
    }
    .img {
      width: 16px;
      height: 16px;
      background: url(https://img.xmkanshu.com/operateimg/img/img/18697/18697.png)
        no-repeat center center;
      background-size: cover;
    }
  }
}
.input_box {
  padding-top: 18px;
  background-color: #fff;
  padding-left: 10px;
  padding-bottom: 20px;
  .look_input {
    width: 89.3%;
    height: 36px;
    padding-left: 18px;
    color: #757575;
    border: 1px solid #999999;
    display: flex;
    align-items: center;
    margin-bottom: 18px;
    input {
      border: none;
      width: 90%;
      margin: 0;
      padding: 0;
    }
    .img {
      width: 25px;
      height: 15px;
      z-index: 999;
      &.con {
        opacity: 0;
      }
    }
  }
}
.btn {
  padding: 0 10px;
  padding-bottom: 150px;
  background-color: #fff;
  button {
    width: 100%;
    color: #fff;
    font-size: 15px;
    background-color: #f54b41;
    height: 36px;
    border: none;
  }
}
.repair {
  height: 285px;
  background-color: #fff;
}
</style>